<template>
  <view>
    <view class="view_goods_box">
      <view v-for="(item,index) in goodsList" :key="index">
        <!-- 商品item项 -->
        <my_goods :item="item"></my_goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        tableDate:{
          query:'', //查询关键词
          cid:'', //商品id
          pagenum:1,
          pagesize:15
        },
        goodsList:[],//商品列表数据
        total:0,
        isloading:false
      }
    },
    onLoad(option) {
      this.tableDate.query = option.query || ''
      this.tableDate.cid = option.cid || ''
      this.getGoodsList()
    },
    methods: {
      // 请求商品列表
      async getGoodsList(log) {
        this.isloading = true
        const {data:res} = await uni.$http.get('/api/public/v1/goods/search',this.tableDate)
        if (res.meta.status !== 200) return uni.$showMsg()
        // 接口调用成后执行关闭上拉事件
        if(log) {
          log()
          this.isloading = false
          this.total = res.message.total
          this.goodsList = [...res.message.goods,...this.goodsList]
          return
        }
        // 触底更新后，需要进行新旧数据拼接
        this.isloading = false
        this.goodsList = [...this.goodsList,...res.message.goods]
        this.total = res.message.total
      },
      // 上拉刷新
      onReachBottom() {
        // 判断是否还有下一页数据
        if(this.tableDate.pagenum*this.tableDate.pagesize >= this.total) return uni.$showMsg('数据加载完毕!')
        // 节流阀
        if(this.isloading) return
        this.tableDate.pagenum += 1
        this.getGoodsList()
      },
      // 下拉刷新的函数
      onPullDownRefresh() {
        // this.tableDate.pagenum = 1
        // this.total = 0
        // this.isloading = false
        // this.goodsList = []
        
        if(this.isloading) return
        this.tableDate.pagenum += 1
        // 2. 重新发起请求
        this.getGoodsList(() => uni.stopPullDownRefresh())
      },
    },
  }
</script>

<style lang="scss">

</style>
